package com.zbkj.crmeb.wechat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.common.PageParamRequest;
import com.constants.WeChatConstants;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.utils.CrmebUtil;

import com.zbkj.crmeb.wechat.model.WechatQrcode;
import com.zbkj.crmeb.wechat.dao.WechatQrcodeDao;
import com.zbkj.crmeb.wechat.request.UrlLinkRequest;
import com.zbkj.crmeb.wechat.request.WechatQrcodeSearchRequest;
import com.zbkj.crmeb.wechat.service.WeChatService;
import com.zbkj.crmeb.wechat.service.WechatQrcodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
* @author Mr.Zhang
* @Description WechatQrcodeServiceImpl 接口实现
* @since 2020-04-18
*/
@Service
public class WechatQrcodeServiceImpl extends ServiceImpl<WechatQrcodeDao, WechatQrcode> implements WechatQrcodeService {

    @Resource
    private WechatQrcodeDao dao;

    @Autowired
    private WeChatService weChatService;


    /**
    * 列表
    * @param request 请求参数
    * @param pageParamRequest 分页类参数
    * @author Mr.Zhang
    * @since 2020-04-18
    * @return List<WechatQrcode>
    */
    @Override
    public List<WechatQrcode> getList(WechatQrcodeSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return dao.selectList(null);
    }

    @Override
    public CommonResult<String> getUrlLink(UrlLinkRequest request,Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        if(request.getExpireType() == 0){
            map.put("expire_time",request.getExpireTime());
        }else if(request.getExpireType() == 1){
            map.put("expire_interval",request.getExpireInterval());
        }else {
            throw new CrmebException("失效类型参数异常");
        }
        if(StringUtils.isNotBlank(request.getPath())){
            map.put("path",request.getPath());
        }
        if(StringUtils.isNotBlank(request.getQuery())){
            map.put("query",request.getQuery());
        }
        if(StringUtils.isNotBlank(request.getEnvVersion())){
            map.put("env_version",request.getEnvVersion());
        }
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_GET_LINK_URL,map,merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success(jsonObject.get("url_link").toString(),null);
        }
        return CommonResult.failed(jsonObject.get("errmsg")+"");
    }

}

